
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once
#ifndef __RTPolynomial__
#define __RTPolynomial__

#include <math.h>
#include <stdio.h>
#include "RTTypes.h"

namespace RT
{

	/*Class to solve polynomial equation*/
	class RTPolynomial
	{
		protected:
			/*polynomial order*/
			I32 n;

			/*The coefficients of the equation*/
			F64 coef[16];
		public:

			/*Null polynomial*/
			static RTPolynomial NullRTPolynomial;

			/*Default constructor*/
			RTPolynomial ();

			/*Destructor*/
			~RTPolynomial (){}

			/*Constructor for a 6th order polynomial*/
			RTPolynomial (const F64& a6, const F64& a5, const F64& a4, const F64& a3, const F64& a2, const F64& a1, const F64& a0)
			{
				coef[0] = a0;
				coef[1] = a1;
				coef[2] = a2;
				coef[3] = a3;
				coef[4] = a4;
				coef[5] = a5;
				coef[6] = a6;
				n = 6;
			}

			/*Constructor for a 4th order polynomial*/
			RTPolynomial (const F64& a, const F64& b, const F64& c, const F64& d, const F64& e)
			{
				coef[0] = e;
				coef[1] = d;
				coef[2] = c;
				coef[3] = b;
				coef[4] = a;
				n = 4;
			}

			/*Constructor for a 3rd order polynomial*/
			RTPolynomial (const F64& a, const F64& b, const F64& c, const F64& d)
			{
				coef[0] = d;
				coef[1] = c;
				coef[2] = b;
				coef[3] = a;
				n = 3;
			}

			/*Constructor for a 2nd order polynomial*/
			RTPolynomial (const F64& a, const F64& b, const F64& c)
			{
				coef[0] = c;
				coef[1] = b;
				coef[2] = a;
				n = 2;
			}

			/*Constructor for a 1st order polynomial*/
			RTPolynomial (const F64& a, const F64& b)
			{
				coef[0] = b;
				coef[1] = a;
				n = 1;
			}

			/*Constructor for a constant*/
			RTPolynomial (const F64& a)
			{
				coef[0] = a;
				n = 0;
			}

	  /*Return a polynomial component*/
	  F64 &operator[] (I32 i)
	  {
		return coef[i];
	  }

	  /*Return a polynomial component*/
	  const F64 operator[] (I32 i) const
	  {
		return coef[i];
	  }

	  /*Unary add operator*/
	  RTPolynomial operator + () const
	  {
		return *this;
	  }

	  /*Unary substract operator*/
	  RTPolynomial operator - () const;

	  /*Add and assign*/
	  RTPolynomial & operator += (const RTPolynomial &);

	  /*Substract and assign*/
	  RTPolynomial & operator -= (const RTPolynomial &);

	  /*Multiply and assign*/
	  RTPolynomial & operator *= (const RTPolynomial &);

	  /*Multiply and assign with a float*/
	  RTPolynomial & operator *= (const F64 &);

	  /*Divide and assign with a float*/
	  RTPolynomial & operator /= (const F64 &);

	  /*Add two polynomials*/
	  friend RTPolynomial operator + (const RTPolynomial &, const RTPolynomial &);

	  /*Substract two polynomials*/
	  friend RTPolynomial operator - (const RTPolynomial &, const RTPolynomial &);

	  /*Multiply two polynomials*/
	  friend RTPolynomial operator *(const RTPolynomial &, const RTPolynomial &);

	  /*Multiply a polynomial with a float*/
	  friend RTPolynomial operator *(const RTPolynomial &, const F64 &);

	  /*Multiply a polynomial with a float*/
	  friend RTPolynomial operator *(const F64 &, const RTPolynomial &);

	  /*Divide a polynomial with a float*/
	  friend RTPolynomial operator / (const RTPolynomial &, const F64 &);

	  /*Calculate the value of the polynomial function.*/
	  F64 Eval (const F64 &);

	  /*Solve a quadratic equation. Store the result in an array of two floats. Return the number of roots.*/
	  I32 SolveQuadratic (F64 *);

	  /*Solve a cubic equation. Store the result in an array of three floats. Return the number of roots.*/
	  I32 SolveCubic (F64 *);

	  /*Solve a quadratic equation. Store the result in an array of four floats. Return the number of roots.*/
	  I32 SolveQuartic (F64 *);

	  /*Return the order of the polynomial*/
	  I32 Degree ()
	  {
		return n;
	  }

	  /*Check the polynomial and lower it's order if needed*/
	  void Check ();
	};

}

#endif
